
import pygame
import mods.CrystalLib
import mods.printlib
import Shaders.blockworld
import Shaders.jade
from random import randint as ri
#from typing import Literal

_version = 'Iris Shader v3.0' # add jade.py
_version = 'Iris Shader v4.0' # add addons, ...
_screen = None

__Area = (('Blockworld', (300, 120, 600, 80), 0), ('Jade', (300, 220, 600, 80), 1), )
selected_index = 1
shaders_list = [Shaders.blockworld, Shaders.jade]
vanilla = Shaders.blockworld # shaders_list[1]

config_string = ''
config_func = None

def choose_shader(index, isInit = False):
    global vanilla, config_string, config_func, selected_index
    vanilla = shaders_list[index]
    selected_index = index
    config_string, config_func = vanilla.inits(_screen)
    if isInit is True:
        mods.CrystalLib.printm('Successfully loading resource.')

def Shader_config_main(screen):
    global selected_index
    c1, c2 = mods.CrystalLib.RandomColor(), (ri(0, 180), ri(0, 180), ri(0, 180))
    c20 = (ri(210, 255), ri(210, 255), ri(210, 255))
    c40, c4 = mods.CrystalLib.RandomColor(), (ri(0, 180), ri(0, 180), ri(0, 180))
    while True:
        mods.CrystalLib.FPS.tick(8)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                return
                break;
            if event.type == pygame.MOUSEBUTTONDOWN:
                xpos, ypos = event.pos[0], event.pos[1]
                if 800 < xpos < 1200 and 800 < ypos < 900:
                    if config_func != None:
                        config_func()
                for i in range(len(shaders_list)):
                    if __Area[i][2] != selected_index:
                        if __Area[i][1][0]< xpos < __Area[i][1][0]+__Area[i][1][2] and __Area[i][1][1] < ypos < __Area[i][1][1]+__Area[i][1][3]:
                            selected_index = __Area[i][2]
                            choose_shader(__Area[i][2])
                            break
                
        pygame.draw.rect(screen, (255, 255, 255), (0, 0, 1200, 900))
        mods.printlib.CreateButton((300, 20, 600, 80), 'Select Shader', (255, 255, 255), (255, 255, 255), color = c1)
        mods.printlib.CreateButton((800, 800, 400, 100), f'Shader {__Area[selected_index][0]}: {config_string}', c4, c40)
        
        for i in range(len(shaders_list)):
            mods.printlib.CreateButton((__Area[i][1]), __Area[i][0], c2 if __Area[i][2] == selected_index else (255,255,255), c20)
        pygame.display.flip()

def check_display():
    mods.CrystalLib.printm(f'[Iris] Obtain display object: {type(_screen)}')

def inits(scr):
    global _screen
    _screen = scr
    mods.CrystalLib.printm(f'Loading mod: {_version}')
    check_display()
    choose_shader(1, True)
    Shaders.jade.set_init()
    Shaders.jade.sf_main_load()
    #return _version
def on_NewGame():
    if selected_index == 1:
        Shaders.jade.flush_resource_now()

def draw_block(x, y, dtype, screen = None):
    global _screen
    if screen == None:
        screen = _screen
    vanilla.draw_block(x, y, dtype, screen)

def draw_addon(x, y, dtype, screen = None):
    global _screen
    if screen == None:
        screen = _screen
    vanilla.draw_addon(x, y, dtype, screen)

def draw_entity(who, screen = None):
    global _screen
    if screen == None:
        screen = _screen
    vanilla.draw_entity(who, screen)

def draw_item(who, screen = None):
    global _screen
    if screen == None:
        screen = _screen
    vanilla.draw_item(who, screen)

def draw_image(which, pos : tuple, screen = None):
    global _screen
    if screen == None:
        screen = _screen
    vanilla.draw_pic(which, pos, screen)

def draw_particle_enderpearl(x: int, y: int):
    if selected_index == 0:
        pass
    else:
        vanilla.draw_particle_enderpearl(x, y)
Illustrated = lambda: Shaders.jade.Illustrate(_screen)
MA_NULL = Shaders.jade.MA_NULL
MA_APPLE = Shaders.jade.MA_APPLE
MA_POWER = Shaders.jade.MA_POWER
MA_TNT  = Shaders.jade.MA_TNT
MA_SHIELD = Shaders.jade.MA_SHIELD
MA_ROCKET = Shaders.jade.MA_ROCKET
MA_DIAMOND = Shaders.jade.MA_DIAMOND
MA_LANDMINE = Shaders.jade.MA_LANDMINE
mods.CrystalLib.printm(f'Loading shader: {Shaders.blockworld.Shader_name()}')